package com.mybed.crm.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mybed.crm.mapper.BusinessMapper;
import com.mybed.crm.mapper.UserMapper;
import com.mybed.crm.pojo.dto.*;
import com.mybed.crm.pojo.pojo.Business;
import com.mybed.crm.pojo.pojo.BusinessRecord;
import com.mybed.crm.pojo.pojo.Clues;
import com.mybed.crm.pojo.pojo.User;
import com.mybed.crm.pojo.vo.BusinessRecordVo;
import com.mybed.crm.result.PageResult;
import com.mybed.crm.service.BusinessService;
import com.mybed.crm.service.UserService;
import com.mybed.crm.utils.thread.UserThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
//商机管理
public class BusinessServiceImpl extends ServiceImpl<BusinessMapper,Business> implements BusinessService {

    private PageResult pageResult = new PageResult();

    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserService userService;

    /**
     * 分页查询
     * @param pageBusinessDto
     * @return
     */
    @Override
    public PageResult pageBusinessQuery(PageBusinessDto pageBusinessDto) {
        IPage<Business> page = new Page<>(pageBusinessDto.getPageNum(),pageBusinessDto.getPageSize());
        IPage<Business> iPage = businessMapper.pageBusinessQuery(page,null,pageBusinessDto);
        pageResult.insertData(iPage);
        return pageResult;
    }

    /**
     * 公海池的分页差询
     * @param pageBusinessDto
     * @return
     */
    @Override
    public PageResult pageBusinessSeaQuery(PageBusinessDto pageBusinessDto) {
        IPage<Business> page = new Page<>(pageBusinessDto.getPageNum(),pageBusinessDto.getPageSize());
        IPage<Business> iPage = businessMapper.pageBusinessSeaQuery(page,null,pageBusinessDto);
        pageResult.insertData(iPage);
        return pageResult;
    }

    /**
     * 新增商机(后续可能需要新增参数数据)
     * @param businessDto
     */
    @Override
    public void addBusiness(BusinessDto businessDto) {
        //健壮性判断
        if (Objects.isNull(businessDto)){
            return;
        }
        String id = UserThreadLocalUtil.getUser();
        User user = userMapper.selectById(id);
        Business business = new Business();
        //将创建时间和更新时间进行添加
        business.setCreateTime(LocalDateTime.now());
        business.setUpdateTime(LocalDateTime.now());
        //将DTO数据复制到POJO数据中
        BeanUtils.copyProperties(businessDto,business);
        business.setAssignBy(user.getUserName());
        businessMapper.addBusiness(business);
    }

    /**
     * 根据id查询商机(先使用pojo进行返回)
     * @param id
     */
    @Override
    public Business selectBusinessId(Long id) {
        Business business = businessMapper.selectBusinessId(id);
        return business;
    }

    /**
     * 跟进更改商机数据
     * @param businessRecordDto
     */
    @Override
    @Transactional
    public void addRecord(BusinessRecordDto businessRecordDto) {
        User user = userMapper.selectById(UserThreadLocalUtil.getUser());
        BusinessRecord businessRecord = new BusinessRecord();
        Business business = new Business();
        BeanUtils.copyProperties(businessRecordDto,businessRecord);
        businessRecord.setKeyItemsArray(businessRecordDto.getKeyItemsArray().toString());
        businessMapper.addRecord(businessRecord);
        BeanUtils.copyProperties(businessRecord,business);
        business.setId(businessRecord.getBusinessId());
//        business.setNextTime(LocalDateTime.now());
        business.setStatus(2);
        business.setUpdateBy(user.getUserName());
        businessMapper.updateById(business);
    }

    /**
     * 查询跟进后的数据
     * @param businessId
     */
    @Override
    public List<BusinessRecordVo> selectRecord(Long businessId) {
        List<BusinessRecordVo> businessRecordVos = businessMapper.selectRecord(businessId);
        for (BusinessRecordVo businessRecordVo : businessRecordVos) {
            String keyItems = businessRecordVo.getKeyItems();
            List<String> list = Arrays.stream(keyItems.split(","))
                    .collect(Collectors.toList());
            businessRecordVo.setKeys(businessMapper.selectKey(list));
        }
        return businessRecordVos;
    }

    /**
     * 踢回公海
     * @param id
     * @param backReason
     * @return
     */
    @Override
    public void updateBusinessStatus(Integer id, Integer backReason) {
        businessMapper.updateBusinessStatus(id);
    }

    /**
     * 商机分配
     * @param assignmentDto
     */
    @Override
    public void updateBusinessUserId(AssignmentDto assignmentDto) {
        User user = userMapper.selectById(assignmentDto.getUserId());
        assignmentDto.setOwner(user.getUserName());
        assignmentDto.setAssignTime(LocalDateTime.now());
        assignmentDto.setOwnerTime(LocalDateTime.now());
        assignmentDto.setAssignBy(UserThreadLocalUtil.getUser());
        businessMapper.updateBusinessUserId(assignmentDto);
    }

    @Override
    public void gainCluesPool(GainDto gainDto) {
        String userName = userService.getInfo().getUser().getUserName();
        List<Long> ids = gainDto.getIds();
        for (Object id : ids) {
           Business business = new Business();
            business.setId((Long) id);
            business.setStatus(1);
            business.setUpdateBy(userName);
            business.setUpdateTime(LocalDateTime.now());
            businessMapper.updateBusinessStatusId(business);
        }
    }


}
